﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BEGDC.CC.Web.Workflow.Common;
using System.Data;
using ASBCC.Database.Service;
using Telerik.Web.UI;

namespace BEGDC.CC.Web.EZVisit.SystemTools
{
    public partial class FieldUpdate : SearchBasePage
    {
        protected override RadGrid GetGridView()
        {
            return gvMasterData;
        }
        protected override Button GetQueryBtn()
        {
            return btnQuery;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            InitialControl();
            if (!Page.IsPostBack)
            {

            }
        }
        protected void ddlCommandType_SelectedIndexChanged(object sender, EventArgs e)
        {
            string commandType = ddlCommandType.SelectedValue;
            switch (commandType.ToLower())
            {
                case "select":
                    trTableName.Visible = true;
                    trColumnName.Visible = true;
                    trFieldValue.Visible = false;
                    trWhere.Visible = true;
                    break;
                case "update":
                    trTableName.Visible = true;
                    trColumnName.Visible = true;
                    trFieldValue.Visible = true;
                    trWhere.Visible = true;
                    break;
                case "delete":
                    trTableName.Visible = true;
                    trColumnName.Visible = false;
                    trFieldValue.Visible = false;
                    trWhere.Visible = true;
                    break;
                case "insert":
                    trTableName.Visible = true;
                    trColumnName.Visible = true;
                    trFieldValue.Visible = true;
                    trWhere.Visible = true;
                    break;
            }
        }
        protected override DataTable GetDataSource()
        {
            string commandType = ddlCommandType.SelectedValue;
            string sql = string.Empty;
            switch (commandType.ToLower())
            {
                case "select":
                    if (txtColumnName.Text.Trim() != "" && txtTableName.Text.Trim() != "" && txtWhere.Text.Trim() != "")
                        sql = string.Format("select {0}, * from {1} where {2} ", txtColumnName.Text.Trim(), txtTableName.Text.Trim(), txtWhere.Text.Trim());
                    break;
                case "update":
                    if (txtColumnName.Text.Trim() != "" && txtTableName.Text.Trim() != "" && txtWhere.Text.Trim() != "")
                        sql = string.Format("select {0}, * from {1} where {2} ", txtColumnName.Text.Trim(), txtTableName.Text.Trim(), txtWhere.Text.Trim());
                    break;
                case "delete":
                    if (txtTableName.Text.Trim() != "" && txtWhere.Text.Trim() != "")
                        sql = string.Format("select * from {0} where {1} ", txtTableName.Text.Trim(), txtWhere.Text.Trim());
                    break;
                case "insert":
                    if (txtColumnName.Text.Trim() != "" && txtTableName.Text.Trim() != "" && txtWhere.Text.Trim() != "" && txtFieldValue.Text.Trim() != "")
                        sql = string.Format("select {0}, * from {1} where {2} ", txtColumnName.Text.Trim(), txtTableName.Text.Trim(), txtWhere.Text.Trim());
                    break;


            }


            DataTable dt = ProcedureManager.usp3_Select_ExecuteSql(sql).Tables[0];
            return dt;
        }
        protected void btnExecute_Click(object sender, EventArgs e)
        {
            string commandType = ddlCommandType.SelectedValue;
            string sql = string.Empty;
            switch (commandType.ToLower())
            {
                case "select":
                    if (txtColumnName.Text.Trim() != "" && txtTableName.Text.Trim() != "" && txtWhere.Text.Trim() != "")
                        sql = string.Format("select {0} from {1} where {2} ", txtColumnName.Text.Trim(), txtTableName.Text.Trim(), txtWhere.Text.Trim());
                    break;
                case "update":
                    if (txtFieldValue.Text.Trim() != "" && txtColumnName.Text.Trim() != "" && txtTableName.Text.Trim() != "" && txtWhere.Text.Trim() != "" && txtColumnName.Text.Trim().Split(',').Length == txtFieldValue.Text.Trim().Split(',').Length)
                        sql = string.Format("update {0} set {1} = {2} where {3} ", txtTableName.Text.Trim(), txtColumnName.Text.Trim(), txtFieldValue.Text.Trim(), txtWhere.Text.Trim());
                    break;
                case "delete":
                    if (txtTableName.Text.Trim() != "" && txtWhere.Text.Trim() != "")
                        sql = string.Format("delete from {0} where {1} ", txtTableName.Text.Trim(), txtWhere.Text.Trim());
                    break;
                case "insert":
                    if (txtTableName.Text.Trim() != "" && txtColumnName.Text.Trim() != "" && txtFieldValue.Text.Trim() != "")
                        sql = string.Format("insert into {0}({1}) values({2}) ", txtTableName.Text.Trim(), txtColumnName.Text.Trim(), txtFieldValue.Text.Trim());
                    break;

            }
            DataTable dt = ProcedureManager.usp3_Select_ExecuteSql(sql).Tables[0];

            gvMasterData.Rebind();
        }
    }
}
